﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DataLib.DBUtility;
namespace DataLib.DAL
{
	/// <summary>
	/// 数据访问类:Directsell
	/// </summary>
	public partial class Directsell
	{
		public Directsell()
		{}
        #region  BasicMethod

        /// <summary>
        /// 得到最大ID
        /// </summary>
        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("id", "Directsell");
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int? id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Directsell");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(DataLib.Model.Directsell model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Directsell(");
            strSql.Append("loadnumid,customerid,customerName,saleMan,Describe,goodsWeight,Remark,loadnumno,customerCode,stepsell,cjoid,cjono)");
            strSql.Append(" values (");
            strSql.Append("@loadnumid,@customerid,@customerName,@saleMan,@Describe,@goodsWeight,@Remark,@loadnumno,@customerCode,@stepsell,@cjoid,@cjono)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@loadnumid", SqlDbType.Int,4),
					new SqlParameter("@customerid", SqlDbType.Int,4),
					new SqlParameter("@customerName", SqlDbType.NVarChar,30),
					new SqlParameter("@saleMan", SqlDbType.NVarChar,30),
					new SqlParameter("@Describe", SqlDbType.NVarChar,200),
					new SqlParameter("@goodsWeight", SqlDbType.Decimal,9),
					new SqlParameter("@Remark", SqlDbType.NVarChar,200),
					new SqlParameter("@loadnumno", SqlDbType.NVarChar,30),
					new SqlParameter("@customerCode", SqlDbType.NVarChar,30),
					new SqlParameter("@stepsell", SqlDbType.NVarChar,20),
                    new SqlParameter("@cjoid", SqlDbType.Int),
                    new SqlParameter("@cjono", SqlDbType.VarChar,30)};
            parameters[0].Value = model.loadnumid;
            parameters[1].Value = model.customerid;
            parameters[2].Value = model.customerName;
            parameters[3].Value = model.saleMan;
            parameters[4].Value = model.Describe;
            parameters[5].Value = model.goodsWeight;
            parameters[6].Value = model.Remark;
            parameters[7].Value = model.loadnumno;
            parameters[8].Value = model.customerCode;
            parameters[9].Value = model.stepsell;
            parameters[10].Value = model.cjoid;
            parameters[11].Value = model.cjono;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(DataLib.Model.Directsell model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Directsell set ");
            strSql.Append("loadnumid=@loadnumid,");
            strSql.Append("customerid=@customerid,");
            strSql.Append("customerName=@customerName,");
            strSql.Append("saleMan=@saleMan,");
            strSql.Append("Describe=@Describe,");
            strSql.Append("goodsWeight=@goodsWeight,");
            strSql.Append("Remark=@Remark,");
            strSql.Append("loadnumno=@loadnumno,");
            strSql.Append("customerCode=@customerCode,");
            strSql.Append("stepsell=@stepsell,");
            strSql.Append("cjoid=@cjoid,");
            strSql.Append("cjono=@cjono");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@loadnumid", SqlDbType.Int,4),
					new SqlParameter("@customerid", SqlDbType.Int,4),
					new SqlParameter("@customerName", SqlDbType.NVarChar,30),
					new SqlParameter("@saleMan", SqlDbType.NVarChar,30),
					new SqlParameter("@Describe", SqlDbType.NVarChar,200),
					new SqlParameter("@goodsWeight", SqlDbType.Decimal,9),
					new SqlParameter("@Remark", SqlDbType.NVarChar,200),
					new SqlParameter("@loadnumno", SqlDbType.NVarChar,30),
					new SqlParameter("@customerCode", SqlDbType.NVarChar,30),
					new SqlParameter("@stepsell", SqlDbType.NVarChar,30),
					new SqlParameter("@id", SqlDbType.Int,4),
                    new SqlParameter("@cjoid", SqlDbType.Int,4),
                    new SqlParameter("@cjono", SqlDbType.VarChar,30)};
            parameters[0].Value = model.loadnumid;
            parameters[1].Value = model.customerid;
            parameters[2].Value = model.customerName;
            parameters[3].Value = model.saleMan;
            parameters[4].Value = model.Describe;
            parameters[5].Value = model.goodsWeight;
            parameters[6].Value = model.Remark;
            parameters[7].Value = model.loadnumno;
            parameters[8].Value = model.customerCode;
            parameters[9].Value = model.stepsell;
            parameters[10].Value = model.id;
            parameters[11].Value = model.cjoid;
            parameters[12].Value = model.cjono;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int? id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Directsell ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Directsell ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DataLib.Model.Directsell GetModel(int? id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,loadnumid,customerid,customerName,saleMan,Describe,goodsWeight,Remark,loadnumno,customerCode,stepsell,cjoid,cjono from Directsell ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
            parameters[0].Value = id;

            DataLib.Model.Directsell model = new DataLib.Model.Directsell();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DataLib.Model.Directsell DataRowToModel(DataRow row)
        {
            DataLib.Model.Directsell model = new DataLib.Model.Directsell();
            if (row != null)
            {
                if (row["id"] != null && row["id"].ToString() != "")
                {
                    model.id = int.Parse(row["id"].ToString());
                }
                if (row["loadnumid"] != null && row["loadnumid"].ToString() != "")
                {
                    model.loadnumid = int.Parse(row["loadnumid"].ToString());
                }
                if (row["customerid"] != null && row["customerid"].ToString() != "")
                {
                    model.customerid = int.Parse(row["customerid"].ToString());
                }
                if (row["customerName"] != null)
                {
                    model.customerName = row["customerName"].ToString();
                }
                if (row["saleMan"] != null)
                {
                    model.saleMan = row["saleMan"].ToString();
                }
                if (row["Describe"] != null)
                {
                    model.Describe = row["Describe"].ToString();
                }
                if (row["goodsWeight"] != null && row["goodsWeight"].ToString() != "")
                {
                    model.goodsWeight = decimal.Parse(row["goodsWeight"].ToString());
                }
                if (row["Remark"] != null)
                {
                    model.Remark = row["Remark"].ToString();
                }
                if (row["loadnumno"] != null)
                {
                    model.loadnumno = row["loadnumno"].ToString();
                }
                if (row["customerCode"] != null)
                {
                    model.customerCode = row["customerCode"].ToString();
                }
                if (row["stepsell"] != null)
                {
                    model.stepsell = row["stepsell"].ToString();
                }
                if (row["cjoid"] != null)
                {
                    model.cjoid = int.Parse(row["cjoid"].ToString());
                }
                if (row["cjono"] != null)
                {
                    model.cjono = row["cjono"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,loadnumid,customerid,customerName,saleMan,Describe,goodsWeight,Remark,loadnumno,customerCode,stepsell,cjoid,cjono ");
            strSql.Append(" FROM Directsell ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,loadnumid,customerid,customerName,saleMan,Describe,goodsWeight,Remark,loadnumno,customerCode,stepsell,cjoid,cjono ");
            strSql.Append(" FROM Directsell ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Directsell ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM ( ");
            strSql.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                strSql.Append("order by T." + orderby);
            }
            else
            {
                strSql.Append("order by T.id desc");
            }
            strSql.Append(")AS Row, T.*  from Directsell T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                strSql.Append(" WHERE " + strWhere);
            }
            strSql.Append(" ) TT");
            strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Directsell";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

